<?php 
include_once('../includes.php');	
if($_SERVER["REQUEST_METHOD"]=="POST")
{
	$value = $_POST["value"] ;
	$target_id = $_POST["target_id"] ;
	
	//update rating
	include_once('rating.class.php');	
	$rating = new Rating();
	$rating->target_id = $target_id;
	
	$result = $rating->updateRating($value);

	echo json_encode($result);
	exit;
}

if($_GET["action"] == "getRating")
{
	$target_id = $_GET["target_id"] ;
	
	//update rating
	include_once('rating.class.php');	
	$rating = new Rating();
	$rating->target_id = $target_id;
	
	$rating->getRating();
	
	$avg = ($rating->total_votes > 0)? ($rating->total_value / $rating->total_votes ) : 0 ;
	$avg = round($avg,1);
	
	$result = array(
		'votes' => $rating->total_votes ,
		'sum' => $rating->total_value, 
		'avg' => $avg
	);
	
	
	echo json_encode($result);
	exit;
}

?>